<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: x4you
 * COPYRIGHT	: 2009
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 * 
 * </pre>
 * 
 * Created on	:  Mar 11, 2009
 * 
 * @author		hoadn <hoadn@rubik.com.vn>
 * @access		[public/private]
 * @package	[package name]
 *
 */
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RCoreImporter::cls( "mvc.AbstractController" );

RClassesImporter::cls('dbo.dtos.LabelKeyDTO');

class LabelController extends AbstractController {

	/**
	 * Model of EventModels
	 *
	 * @var EventModel
	 */
	var $_model	=	NULL;
	
	/**
	 * Contrustor of EventController
	 *
	 */
	function __construct() {
		parent::__construct ();
		SysApplication::importModelCls ( "LanguageModel" );
		$this->_model = new LanguageModel ( );
		$this->registerTask ( "new", "edit" );
		$this->registerTask ( "apply", "save" );
	}

	function display() {
		$this->setTitle ( SysLanguage::_('LABEL_MANAGEMENT') );
//		$this->addToolbarButton ( 'new' );
		$this->addToolbarButton ( 'edit' );
		$this->addToolbarButton ( 'delete' );
		$msg = SysRequest::getVar ( "msg" );
		RClassesImporter::cls('dbo.views.LabelKeyViewDTO');
		RCoreImporter::cls( "html.TableControl" );
		
		$table = new TableControl ( 'LabelKeyViewDTO', NULL, $_REQUEST, 'CountryTbl', '', 'id DESC' );
		$table->setLinkRule('key', 'javascript:editTaskItem($key);');
		$res = SysRequest::cloneResource();
		$table->removeField('id');
		echo "<p class=\"form-message\" style=\"text-align:left;\">
					{$msg}
				</p>";
		$table->printHtmlTable ( 20 );
	}
	
	/**
	 * Function edit Event
	 *
	 * @param LabelKeyDTO $dto
	 */
	function edit( $dto = null, $richText = false )
	{
		$this->setTitle ( SysLanguage::_('EDIT_LABEL') );
		if ($richText) {
			$this->addToolbarButton ( "normalEdit", "Pure Text Edit");
		}
		else {
			$this->addToolbarButton ( "richEdit", "Rich Text Edit");
		}
		$this->addToolbarButton ( "save" );
		$this->addToolbarButton ( "apply" );
		$this->addToolbarButton ( "cancel" );
		$msg = SysRequest::getVar ( "msg" );
		$oModel = $this->_model;
		$cid = SysRequest::getVar ( "cid" );
		if (empty( $dto )) { 
			if (empty ( $cid )) {
					$this->setTitle ( SysLanguage::_('NEW_LABEL') );
					$isNew = true;
			}
			if(is_array($cid)) {
				$cid = $cid[0];
			}				
			$dto = $oModel->loadLabelDTO($cid);
		}
		
		RCoreImporter::cls( "html.FormFieldsControl" );			
		$formData = new FormFieldsControl ( $dto );
		
		if ($richText) {
			// set rich text controls
			$listLangs = $oModel->getSiteLangCodeList();
			foreach ($listLangs as $item) {
				$key = "label_{$item[0]}";
				$formData->setFieldHtmlInfo($key, $this->_getRichTextHtmlInfo($key));
			}
		}
		
		$htmlContent = $formData->generateHtml();
		eval('echo "'.SysApplication::loadTemplate('back/editLabel').'";');	
	}
	
	/**
	 * Enter description here...
	 *
	 * @return RichTextHtmlFieldInfo
	 */
	function &_getRichTextHtmlInfo($inputName) {
		RCoreImporter::cls('html.fieldInfo.RichTextHtmlFieldInfo');
		$info = new RichTextHtmlFieldInfo($inputName);						
		return $info;
	}
	
	function richEdit() {
		$this->edit(null, true);
	}
	
	function normalEdit() {
		$this->edit(null, false);
	}
	
	function save() {
		$oModel = $this->_model;		
		$dto = new LabelKeyDTO();
		$dto->bind($_POST);
		
		// Set value for all missed data
		$config = SystemSite::getSiteConfig();
		$key = "label_{$config->language}";
		$defaultVal = $dto->$key;
		
		$listLangs = $oModel->getSiteLangCodeList();
		foreach ($listLangs as $item) {
			$key = "label_{$item[0]}";
			if (empty($dto->$key)) {
				$dto->$key = $defaultVal;
			}
		}
		
		// set the processed equal true
		$dto->processed = true;
		
		$result = $oModel->saveDTO ( $dto );										
		if ($dto->hasError())
			$msg = $dto->getAllErrorsMsg();			
		
		if ($result === false || $this->getExecuteVal () == 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit ($dto);
		} else {
			$this->setRedirectToFunction('display', $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" ));			
		}				
	}
	
	/**
	 * Function 
	 *
	 * @return boolean
	 */
	function delete() {
		$oModel = $this->_model;
		$cid = SysRequest::getVar ( "cid" );
		if($oModel->deleteLabel($cid)) {
			$this->setRedirectToFunction('display', $msg ? $msg : SysLanguage::_ ( "ITEM_DELETE_SUCCESSFUL" ));		
		}
	}
	
}
?>